package com.example.framework;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

public class HelperWithWebDriverBase {

	protected final ApplicationManager manager;
	private WebDriver driver;
	

	public HelperWithWebDriverBase(ApplicationManager manager){
		this.manager = manager;
		driver = manager.getWebDriverHelper().getDriver();
	}
	
	protected void type(By locator, String value) {
		findElement(locator).clear();
		findElement(locator).sendKeys(value);
	}

	protected void selectItemFromDropDownList(By dropDownListLocator,
			String itemToSelect) {
		new Select(findElement(dropDownListLocator))
				.selectByVisibleText(itemToSelect);
	}

	protected boolean isElementPresent(By by) {
		try {
			findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}

	protected WebElement findElement(By linkText) {
		return driver.findElement(linkText);
	}

	protected void openUrl(String url) {
		driver.get(manager.getWebDriverHelper().baseUrl + url);
	}

	protected void click(By name) {
		findElement(name).click();
	}
}
